package com.zhouwen.ssm.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zhouwen.ssm.domain.Commodity;
import com.zhouwen.ssm.domain.VO;
import com.zhouwen.ssm.service.ICommodityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/commodity")
public class CommodityController {

    @Autowired
    private ICommodityService commodityService;

    @RequestMapping("/test23")
    @ResponseBody
    public void findById23(String username, MultipartFile[] uploadFiles) throws IOException {
        System.out.println(username);
        for (MultipartFile uploadFile : uploadFiles) {
            //获得上传文件的名称
            String filename = uploadFile.getOriginalFilename();
            uploadFile.transferTo(new File("C:\\Users\\周文\\OneDrive\\桌面\\新建文件夹\\" + filename));
        }
    }

    /**
     * 表单项type= "file"
     * 表单的提交方式是post。
     * 表单的enctype属性是多部分表单形式，以及enctype= "multipart/form-data"。
     * <p>
     * 导入fileupload和io坐标
     * 配置文件上传解析器
     * 编写文件上传代码
     */
    @RequestMapping("/test22")
    @ResponseBody
    public void findById22(String username, MultipartFile uploadFile) throws IOException {
        System.out.println(username);
        //获得上传文件的名称
        String filename = uploadFile.getOriginalFilename();
        uploadFile.transferTo(new File("C:\\Users\\周文\\OneDrive\\桌面\\新建文件夹\\" + filename));
    }

    @RequestMapping("/test21")
    @ResponseBody
    public void findById21(@CookieValue(value = "JSESSIONID") String jsessionId) {
        System.out.println(jsessionId);
    }

    /**
     * User-Agent在请求头里面
     * required = false代表就算请求头没有User-Agent也能访问该方法
     *
     * @param user_agent
     */
    @RequestMapping("/test20")
    @ResponseBody
    public void findById20(@RequestHeader(value = "User-Agent", required = false) String user_agent) {
        System.out.println(user_agent);
    }

    /**
     * 获取servlet相关的API
     *
     * @param request
     * @param response
     * @param session
     */
    @RequestMapping("/test19")
    @ResponseBody
    public void findById19(HttpServletRequest request, HttpServletResponse response, HttpSession session) {
        System.out.println(request);
        System.out.println(response);
        System.out.println(session);
    }

    @RequestMapping("/test18")
    @ResponseBody
    public void findById18(Date date) {
        System.out.println(date);
    }

    /**
     * Restful编程风格  url + 请求方式
     * POST 用于新增资源
     * PUT  用于修改资源
     * GET  用于获得资源
     * DELETE  用于删除资源
     * <p>
     * /test17/{name}  // {name}是占位符
     * test17/张三
     *
     * @param username
     */
    @RequestMapping("/test17/{name}")
    @ResponseBody
    public void findById17(@PathVariable(value = "name", required = true) String username) {
        System.out.println(username);
    }

    /**
     * test16?name=张三
     *
     * @param username
     */
    @RequestMapping("/test16")
    @ResponseBody
    public void findById16(@RequestParam("name") String username) {
        System.out.println(username);
    }

    /**
     * ajax.jsp页面请求
     *
     * @param commodityList
     */
    @RequestMapping("/test15")
    @ResponseBody
    public void findById15(@RequestBody List<Commodity> commodityList) {
        System.out.println(commodityList);
    }

    /**
     * from.jsp页面请求
     *
     * @param vo
     */
    @RequestMapping("/test14")
    @ResponseBody
    public void findById14(VO vo) {
        System.out.println(vo);
    }

    /**
     * test13?strs=111&strs=aaa&strs=bbb
     *
     * @param strs
     */
    @RequestMapping("/test13")
    @ResponseBody
    public void findById13(String[] strs) {
        System.out.println(Arrays.asList(strs));
    }

    /**
     * 封装到一个实体中，test12?name=zhangsan&price=134
     *
     * @param commodity
     */
    @RequestMapping("/test12")
    @ResponseBody
    public void findById12(Commodity commodity) {
        System.out.println(commodity.getName());
        System.out.println(commodity.getPrice());
    }

    /**
     * test11?username=zhangsan&age=11
     *
     * @param username
     * @param age
     */
    @RequestMapping("/test11")
    @ResponseBody
    public void findById11(String username, int age) {
        System.out.println(username);
        System.out.println(age);
    }

    /**
     * springMVC自动将对象或集合转换为json字符串，需在springmvc.xml文件中配置处理器映射器
     * 或者在直接springmvc.xml文件中加上<mvc:annotation-driven />
     *
     * @return
     */
    @RequestMapping("/test10")
    @ResponseBody   //返回字符串，告诉spring框架不要进行跳转
    public Commodity findById10() {
        Commodity commodity = commodityService.findById(1);
        return commodity;
    }

    @RequestMapping("/test9")
    @ResponseBody   //返回字符串，告诉spring框架不要进行跳转
    public String findById9() throws JsonProcessingException {
        Commodity commodity = commodityService.findById(1);
        //使用json的转换工具将对象转换为json对象返回
        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(commodity);
        return json;
    }

    @RequestMapping("/test8")
    @ResponseBody   //返回字符串，告诉spring框架不要进行跳转
    public String findById8() {
        return "{\"username\":\"张三\",\"age\":18}";
    }

    @RequestMapping("/test7")
    @ResponseBody   //返回字符串，告诉spring框架不要进行跳转
    public String findById7() {
        return "hello zhouwen!!!";
    }

    @RequestMapping("/test6")
    public void findById6(HttpServletResponse response) throws IOException {
        response.getWriter().print("hello !!!");
    }

    @RequestMapping("/test5")
    public String findById5(HttpServletRequest request) {
        request.setAttribute("zw", "zzzzezzz");
        return "findById";
    }

    @RequestMapping("/test4")
    public String findById4(Model m) {
        m.addAttribute("zw", "zhouwen");
        return "findById";
    }

    @RequestMapping("/test3")
    public ModelAndView findById3(ModelAndView mv) {
        mv.setViewName("findById");
        return mv;
    }

    @RequestMapping("/test2")
    public String findById2(int id) {
        return "findById";
    }

    /**
     * method = RequestMethod.POST
     * 请求方式必须是post
     * <p>
     * params = {"id!0"}
     * 指定限制请求参数的条件 id不能是0
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/test1", method = RequestMethod.POST, params = {"id!=0"})
    public ModelAndView findById(int id) {
        ModelAndView mv = new ModelAndView();
        Commodity commodity = commodityService.findById(id);
        mv.addObject("commodity", commodity);
        mv.setViewName("findById");
        return mv;
    }
}
